<template>
    <div>
        <el-card>
            <el-row :gutter="12">
                <el-col :span="2">
                    <el-button type="primary" @click="newShippingTemplate"
                        >添加快递模板</el-button
                    >
                </el-col>
            </el-row>
            <el-table :data="shippingTemplateList" border stripe>

                <el-table-column label="id" prop="id"></el-table-column>
                <el-table-column label="快递模板名称" prop="name"></el-table-column>
                <el-table-column label="标准费用" prop="price"></el-table-column>
                <el-table-column label="满多少" prop="exceedPrice"></el-table-column>
                <el-table-column label="满后费用" prop="exceedAfterPrice"></el-table-column>
                <el-table-column label="支持满100免费" prop="supportExceedFree"></el-table-column>
                <el-table-column label="区域设置">
                    <template slot-scope="sc">
                        <el-button type="warning" plain size="mini" @click="queryShippingRegionPriceList(sc.row.id)">区域设置</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="添加时间" prop="addTime"></el-table-column>

                <el-table-column label="编辑">
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            icon="el-icon-edit"
                            size="mini"
                            @click="editShippingTemplate(scope.row)"
                        ></el-button>
                    </template>
                </el-table-column>
                <!-- 分页 -->
            </el-table>

        </el-card>
        <el-dialog title="修改快递模板" :visible.sync="addShippingTemplateVisible" width="60%">
            <el-form :model="addShippingTemplateForm" ref="addShippingTemplateRef" :rules="addShippingTemplateRules" label-width="150px">
                <el-form-item label="快递模板名称" prop="name">
                    <el-input
                        v-model="addShippingTemplateForm.name"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input
                        v-model="addShippingTemplateForm.price"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
                <el-form-item label="订单金额满" prop="exceedPrice">
                    <el-input
                        v-model="addShippingTemplateForm.exceedPrice"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
                <el-form-item label="满后金额" prop="exceedPrice">
                    <el-input
                        v-model="addShippingTemplateForm.exceedAfterPrice"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
                <el-form-item label="满100免费" prop="exceedPrice">
                     <el-select v-model="addShippingTemplateForm.supportExceedFree"  style="wdith:120px;">
                            <el-option
                                key="Y"
                                label="支持"
                                value="Y">
                            </el-option>
                            <el-option
                                key="N"
                                label="不支持"
                                value="N">
                            </el-option>
                        </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addShippingTemplateVisible = false">取 消</el-button>
                <el-button type="primary" @click="addShippingTemplate">确 定</el-button>
            </span>
        </el-dialog>
        <shipping-region-price ref="shippingRegionPrice"></shipping-region-price>
    </div>
</template>

<script>
import MallApi from "@/api/Mall.js";
import ShippingRegionPrice from './ShippingRegionPrice.vue';
export default {
  components: { ShippingRegionPrice },
    data() {
        return {
            queryInfo: {
                ShippingTemplateName: "",
                pagenum: 1, // 页码
                pagesize: 10, // 页大小
            },
            shippingTemplateList: [],
            addShippingTemplateVisible: false,
            addShippingTemplateForm: {
                id: "",
                name: "",
                price: "",
                supportExceedFree:"Y"

            },
            addShippingTemplateRules: {
                name: [
                    {
                        required: true,
                        message: "请输入快递模板名字",
                        trigger: "change",
                    }
                ],
                price: [
                    {
                        required: true,
                        message: "请输入快递费用",
                        trigger: "change",
                    }
                ]
            }
        };
    },
    watch: {

    },
    created() {
        this.getShippingTemplateList();
    },
    methods: {
        getShippingTemplateList() {
          MallApi.getShippingTemplateList(this.queryInfo).then((res) => {
                if (res.success) {
                    this.shippingTemplateList = res.data.shippingTemplateList;
                } else {
                    return this.$message.error("获取快递模板列表失败");
                }
            });
        },
        newShippingTemplate() {
            this.addShippingTemplateVisible = true;
            this.addShippingTemplateForm = {
               id: "",
                name: "",
                price: ""
            };
        },
        editShippingTemplate(info) {
          MallApi.getShippingTemplateDetail({
                id: info.id,
            }).then((res) => {
                if (res.success) {
                    this.addShippingTemplateVisible = true;
                    this.addShippingTemplateForm = JSON.parse(JSON.stringify(res.data));
                }
            });
        },

        // 添加
        addShippingTemplate() {
            this.$refs.addShippingTemplateRef.validate((valid) => {
                if (valid) {
                  MallApi.saveShippingTemplate(this.addShippingTemplateForm).then((res) => {
                        if (res.success) {
                            this.addShippingTemplateVisible = false;
                            this.getShippingTemplateList();
                        }
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        queryShippingRegionPriceList(templateId) {
            this.$refs.shippingRegionPrice.showShippingRegionPriceList(templateId);
        }
    }
};
</script>

<style>
.form_ipt {
    width: 450px;
}
</style>
